﻿using Coldairarrow.Business.Base_leasesystem_service;
using Coldairarrow.Entity.Base_leasesystem_service;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using Aspose.Cells;
using NPOI.Util;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Threading.Tasks;
using Coldairarrow.Entity.ViewModel;
using Coldairarrow.Util.Helper;
using NSwag.Annotations;
using System;

namespace Coldairarrow.Api.Controllers.Base_leasesystem_service
{
    /// <summary>
    /// 服务商控制器
    /// </summary>
    [Route("/Base_leasesystem_service/[controller]/[action]")]
    [OpenApiTag("服务商控制器")]
    public class leasesystem_serviceController : BaseApiController
    {
        #region DI

        public leasesystem_serviceController(Ileasesystem_serviceBusiness leasesystem_serviceBus)
        {
            _leasesystem_serviceBus = leasesystem_serviceBus;
        }

        Ileasesystem_serviceBusiness _leasesystem_serviceBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<leasesystem_service>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _leasesystem_serviceBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<leasesystem_service> GetTheData(IdInputDTO input)
        {
            return await _leasesystem_serviceBus.GetTheDataAsync(input.id);
        }

        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(leasesystem_service data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);
                data.FileAttId = IdHelper.GetId();

                await _leasesystem_serviceBus.AddDataAsync(data);
            }
            else
            {
                await _leasesystem_serviceBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        [ApiPermission("service.Delete")]
        public async Task DeleteData(List<string> ids)
        {
            await _leasesystem_serviceBus.DeleteDataAsync(ids);
        }

        /// <summary>
        /// 读取上传出租人信息并返回结果
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object UploadExcel()
        {
            var files = Request.Form.Files[0];
            var ms = new MemoryStream();
            files.CopyTo(ms);

            DataTable dt = new DataTable();
            Workbook book = new Workbook(ms);
            Worksheet sheet = book.Worksheets[0];
            Cells cells = sheet.Cells;

            dt =  cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);
            return _leasesystem_serviceBus.SettlementExcel(dt);
        }

        
        #endregion
    }

}